package com.resume.ex;

import com.resume.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.text.MessageFormat;

/**
 * 全局异常
 *
 * @author 梁云亮
 */
@ApiModel(value = "自定义全局异常")
@Getter
@NoArgsConstructor
@ToString
public class ResumeException extends RuntimeException {

    @ApiModelProperty(value = "异常枚举")
    private ResultEnum resultEnum;

    public ResumeException(String msg) {
        super(msg);
    }

    /**
     * 接收状态码和消息
     *
     * @param code
     * @param msg
     */
    public ResumeException(Integer code, String msg) {
        super(msg);
        this.resultEnum.setMsg(msg);
        this.resultEnum.setCode(code);
    }

    /**
     * 接收枚举类型参数
     *
     * @param resultEnum
     */
    public ResumeException(ResultEnum resultEnum) {
        super(resultEnum.getMsg());
        this.resultEnum = resultEnum;
    }

    public ResumeException(ResultEnum resultEnum, Object... args) {
        super(resultEnum.getMsg());
        String message = MessageFormat.format(resultEnum.getMsg(), args);
        this.resultEnum.setMsg(message);
        this.resultEnum = resultEnum;
    }

    public ResultEnum getResultEnum() {
        return this.resultEnum;
    }
}
